'''
Created on Jul 2, 2011

@author: pau <boemianrapsodi@gmail.com>
'''

from xml.dom.minidom import Document, parse;
import os;

class ConfigurationController:
    PATH= os.path.abspath(__file__+"/../../../etc/LocalMarketServer.conf");
    configuration=None;
    
    packetsDir=None;
    serverHost=None;
    serverPort=None;
    serverName=None;
    
    def __init__(self):
        try:
            self.configuration = parse(self.PATH);
            print "Configuration File Loaded";
            self.packetsDir = self.configuration.getElementsByTagName("packetsDir")[0].childNodes[0].nodeValue.strip();
            self.serverHost = self.configuration.getElementsByTagName("serverHost")[0].childNodes[0].nodeValue.strip();
            self.serverPort = self.configuration.getElementsByTagName("serverPort")[0].childNodes[0].nodeValue.strip();
            self.serverName = self.configuration.getElementsByTagName("serverName")[0].childNodes[0].nodeValue.strip();
        except Exception, inst:
            print "ERROR: %s - %s" % (self.PATH, inst);
            print "Regenerating config file...";
            self.configuration = self.RenewConfiguration();
            self.SaveConfigToFile();
            print "done";
        

    def RenewConfiguration(self):
        defaultConfiguration = Document();
        root = defaultConfiguration.createElement("LocalMarketServerConfigurationFile");
        defaultConfiguration.appendChild(root);
        
        repoConfigurations = defaultConfiguration.createElement("RepoConfigurations");
        root.appendChild(repoConfigurations);
        packetsDir = defaultConfiguration.createElement("packetsDir");
        packetsDirValue = defaultConfiguration.createTextNode(os.path.abspath(__file__+"/../../../pkts/"));
        packetsDir.appendChild(packetsDirValue);
        repoConfigurations.appendChild(packetsDir);
        
        serverConfiguration = defaultConfiguration.createElement("ServerConfiguration");
        root.appendChild(serverConfiguration);
        serverHost = defaultConfiguration.createElement("serverHost");
        serverHostValue = defaultConfiguration.createTextNode("localhost");
        serverHost.appendChild(serverHostValue);
        serverConfiguration.appendChild(serverHost);
        serverPort = defaultConfiguration.createElement("serverPort");
        serverPortValue = defaultConfiguration.createTextNode("21567");
        serverPort.appendChild(serverPortValue);
        serverConfiguration.appendChild(serverPort);
        
        serverInfo = defaultConfiguration.createElement("ServerInfo");
        root.appendChild(serverInfo);
        serverName = defaultConfiguration.createElement("serverName");
        serverNameValue = defaultConfiguration.createTextNode("Default Local Market");
        serverName.appendChild(serverNameValue);
        serverInfo.appendChild(serverName);
        serverIcon = defaultConfiguration.createElement("serverIcon");
        serverIconValue = defaultConfiguration.createTextNode(os.path.abspath(__file__+"/../../../res/serverTitle.png"));
        serverIcon.appendChild(serverIconValue);
        serverInfo.appendChild(serverIcon);
        
        print "Loaded default configuration:";
        print defaultConfiguration.toprettyxml("");

        return defaultConfiguration;
    
    def GetServerAddress(self):
        return self.serverHost;
    
    def GetServerPort(self):
        return self.serverPort;
    
    def GetPacketsDir(self):
        return self.packetsDir;
    
    def GetServerName(self):
        return self.serverName;
         
            
    def SaveConfigToFile(self):
        loadedConfigFile = open(self.PATH, "w");
        try:
            loadedConfigFile.write(self.configuration.toprettyxml(indent=""));
        finally:
            loadedConfigFile.close();



            